package com.xuanzheng.company.servlet;

import com.google.gson.JsonObject;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.CompanyEmployeeService;
import com.xuanzheng.company.service.UserService;
import com.xuanzheng.company.service.impl.CompanyEmployeeServiceImpl;
import com.xuanzheng.company.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/api/admin/deleteUser")
public class AdminDeleteUserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private CompanyEmployeeService companyEmployeeService = new CompanyEmployeeServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        JsonObject result = new JsonObject();

        // 检查是否是管理员
        Object adminObj = req.getSession().getAttribute("admin");
        if (adminObj == null) {
            result.addProperty("success", false);
            result.addProperty("message", "无权限执行此操作");
            out.print(result);
            return;
        }

        // 获取要删除的用户ID
        String userId = req.getParameter("userId");
        if (userId == null || userId.isEmpty()) {
            result.addProperty("success", false);
            result.addProperty("message", "用户ID不能为空");
            out.print(result);
            return;
        }

        try {
            // 检查用户角色
            User user = userService.findById(userId);
            if (user == null) {
                result.addProperty("success", false);
                result.addProperty("message", "用户不存在");
                out.print(result);
                return;
            }

            // 如果是公司负责人，提示使用公司注销功能
            if (user.getRole() == 1) {
                result.addProperty("success", false);
                result.addProperty("message", "该用户是公司负责人，请使用公司注销功能来完成流程");
                out.print(result);
                return;
            }

            // 1. 先让用户退出所有公司
            boolean leaveCompanyResult = companyEmployeeService.removeAllCompanyRelations(userId);
            if (!leaveCompanyResult) {
                result.addProperty("success", false);
                result.addProperty("message", "退出公司失败");
                out.print(result);
                return;
            }

            // 2. 然后注销用户账号
            boolean deleteUserResult = userService.deleteUserById(userId);
            if (deleteUserResult) {
                result.addProperty("success", true);
                result.addProperty("message", "用户注销成功");
            } else {
                result.addProperty("success", false);
                result.addProperty("message", "用户注销失败");
            }
        } catch (Exception e) {
            result.addProperty("success", false);
            result.addProperty("message", "操作失败：" + e.getMessage());
            e.printStackTrace();
        }

        out.print(result);
    }
} 